<?php
/**
 * 人脸签到
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/26
 * Time: 10:29
 */
use OSS\OssClient;
use OSS\Core\OssException;
//允许跨域访问
header('Access-Control-Allow-Origin:*');
//日志模块
load()->func("logging");
global $_W,$_GPC;
$post_data = $GLOBALS['HTTP_RAW_POST_DATA'];
logging_run("云从实时识别接口:识别的数据=>".$GLOBALS['HTTP_RAW_POST_DATA'],"info");
$post = json_decode($post_data,true);
//用户ID
$userId = $post['custId'];
//姓名
$name = $post['custName'];
//设备编号
$deviceId = $post['deviceId'];
//识别时间
$timeStr = time();
logging_run("云从实时识别接口:识别到用户=>".$name,"info");
//获取用户信息
if($deviceId == '13504c348849b627') {//领奖
    //meepo_xianchang_recognize
    $user = pdo_fetch("SELECT * FROM " . tablename($this->user_table) . " WHERE openid=:openid ",array('openid'=>$userId));
    if($user) {
        $recognize_insert_data = array(
            'user_id'       =>  $user['id'],
            'create_time'   =>  time()
        );
        pdo_insert('meepo_xianchang_recognize',$recognize_insert_data);
    }

} else {//签到
    $user = pdo_fetch("SELECT * FROM " . tablename($this->user_table) . " WHERE openid=:openid ",array('openid'=>$userId));
    if(!$user) {
        //用户不存在时，同步用户
        //调用云从接口获取用户信息
        require_once(IA_ROOT . '/framework/function/communication.func.php');
        $resp = http_post_json('http://wy.linewell.com:8580/mobile/getUserInfo',json_encode(array('id' => $userId)));
        $resp_json = json_decode($resp,true);
        logging_run("云从实时识别接口:获取用户信息=>id=".$userId."，响应：".$resp,"info");
        logging_run("云从实时识别接口:识别到头像".$resp_json['data']['pictureName'],"info");
        //上传oss
        /*require_once(IA_ROOT . '/framework/library/alioss/sdk.class.php');
        $oss = new ALIOSS('LTAIpD4u7ZveDJGm', '4aGNyjBZT9CDtzugTAp55mknjV23Er', 'hho-assets.oss-cn-hangzhou.aliyuncs.com');
        $options = array(
            ALIOSS::OSS_FILE_UPLOAD => 'C:/yuncong/FalconServer/app/ROOT/upload/'.$resp_json['data']['pictureName'],
            ALIOSS::OSS_PART_SIZE => 5242880,
        );
        $filename = "weiya/".$userId.".jpg";
        $response = $oss->create_mpu_object('hho-assets', $filename, $options);
        logging_run("上传阿里云结果".json_encode($response),"info");
        */
        logging_run("上传阿里云1","info");
        require_once IA_ROOT . '/framework/library/oss/weiya/Common.php';
        logging_run("上传阿里云2","info");
        $bucket = Common::getBucketName();
        logging_run("上传阿里云3","info");
        $ossClient = Common::getOssClient();
        logging_run("上传阿里云4","info");
        logging_run("上传阿里云5","info");
        $filename = "weiya/".$userId.".jpg";
        logging_run("上传阿里云6".$bucket,"info");
        $result = $ossClient->uploadFile($bucket, $filename, 'C:/yuncong/FalconServer/app/ROOT/upload/'.$resp_json['data']['pictureName']);
        logging_run("上传阿里云7","info");
        logging_run("上传阿里云结果".json_encode($response),"info");
        $user_insert_data = array(
            'weid'          =>  '1',
            'rid'           =>  '10',
            'openid'        =>  $userId,
            'nick_name'      =>  $name,
            'avatar'        =>  'http://assets.waterforall.cn/'.$filename.'/120',
            'sex'           =>  '1',
            'group'         =>  '0',
            'status'        =>  '1',
            'isblacklist'   =>  '1',
            'can_lottory'   =>  '1',
            'nd_id'         =>  '0',
            'createtime'    =>  time(),
            'mobile'        =>  $resp_json['data']['telephone'],
            'realname'      =>  $resp_json['data']['name'],
            'deptname'      =>  $resp_json['data']['deptName']
        );
        pdo_insert('meepo_xianchang_user',$user_insert_data);
        $qd_count = pdo_fetchcolumn('SELECT COUNT(1) FROM '.tablename($this->qd_table) . ' WHERE openid=:openid',array('openid' => $userId));
        if($qd_count == 0) {
            $data = array('weid'=>'1','rid'=>'10','nick_name'=>$user_insert_data['nick_name'],'openid'=>$user_insert_data['openid'],'avatar'=>$user_insert_data['avatar'],'level'=>1,'createtime'=>time());
            pdo_insert('meepo_xianchang_qd',$data);
            $id = pdo_insertid();
            logging_run("云从签到，用户不存在，签到成功!".$id,"info");
        }
    } else {//用户存在时，更新用户状态为1，同时插入签到记录表
        logging_run("云从人脸签步骤1","info");
        $update_data = array(
            'status'    =>  '1'
        );
        logging_run("云从人脸签步骤2","info");
        pdo_update('meepo_xianchang_user',  $update_data,   array('openid'=>$userId));
        logging_run("云从人脸签步骤3","info");
        $qd_count = pdo_fetchcolumn('SELECT COUNT(1) FROM '.tablename($this->qd_table) . ' WHERE openid=:openid',array('openid' => $userId));
        if($qd_count == 0) {
            $data = array('weid'=>'1','rid'=>'10','nick_name'=>$user['nick_name'],'openid'=>$userId,'avatar'=>$user['avatar'],'level'=>1,'createtime'=>time());
            pdo_insert('meepo_xianchang_qd',$data);
            $id = pdo_insertid();
            logging_run("云从签到，存在，签到成功!".$id,"info");
        }
        logging_run("云从人脸签步骤4","info");
    }
}

die(json_encode(array('respCode'=>200,'respDesc'=>'')));
logging_run("云从人脸签到结束，签到用户：".$name,"info");